<template>
	<div>
		<AplidForm ref="formRef" v-bind="BaseFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import { BaseFormConfig } from '../../config'
import { getAreaObj } from '@/utils/util'
import { useCommonStore } from '@/stores/common'
const { setTempParams } = useCommonStore()

const emit = defineEmits(['submit'])
const rowData: any = inject('nowRowData')
const baseData = Object.freeze(JSON.parse(JSON.stringify(rowData.value)))
const formModel: any = ref({
	...rowData.value,
})
const formRef = ref(null)
const onSubmit = (form, done) => {
	const crIds: any = getAreaObj(form.crAreas, 'cr')
	const crNames: any = getAreaObj(form.cr_areas, 'cr')

	const arIds: any = getAreaObj(form.areas, '')
	const arNames: any = getAreaObj(form.areas_name, '')

	const regArea: any = getAreaObj(form.registerOrgArea, 'registerOrg')
	const regAreaNamesObj: any = getAreaObj(form.registerOrgAreaNames, 'registerOrg')

	const params = {
		...form,
		...crIds,
		...crNames,
		...arIds,
		...arNames,
		...regArea,
		...regAreaNamesObj,
	}
	const emptyObj = {
		crAreas: undefined,
		cr_areas: undefined,
		areas: undefined,
		areas_name: undefined,
		registerOrgArea: undefined,
		registerOrgAreaNames: undefined,
		code: undefined,
	}

	const changeObj = {
		data: [],
		dataChangeContentList: [
			{
				changeBefore: {
					...baseData,
					...emptyObj,
				},
				changeAfter: {
					...params,
					...emptyObj,
				},
				changeData: {
					...params,
					...emptyObj,
				},
				code: 3021,
			},
		],
	}

	setTempParams({
		noChange: true,
		name: params.name,
		idCard: params.idCard,
		nowChange: changeObj,
	})
	emit('submit', changeObj)
	done()
}

const onSave = () => {
	formRef.value!.onSubmit()
}
defineExpose({
	onSave,
})
</script>

<style scoped></style>
